package com.qys.training.openapi.config;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientNetworkConfig;
import com.hazelcast.core.HazelcastInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author Winstone
 * @date 2020/8/5 - 3:03 下午
 */
@Configuration
public class HazelcastClientConfig {
    private static final Logger logger = LoggerFactory.getLogger(HazelcastClientConfig.class);

    @Value("${hazelcast.config.groupName}")
    private String groupName;

    @Value("${hazelcast.config.members}")
    private String members;

    @Bean
    public HazelcastInstance hazelcastClient() {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setProperty("hazelcast.logging.type", "slf4j");
        clientConfig.getGroupConfig().setName(groupName);
        ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
        networkConfig.setAddresses(Arrays.asList(members.split(",")))
                .setSmartRouting(true)
                .setRedoOperation(true)
                .setConnectionTimeout(5000)
                .setConnectionAttemptLimit(60);
        HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
        logger.info("Hazelcast OPEN_API 客户端启动成功");

        return client;
    }

}
